/* -+- file  ./kf2/GUI/SettingsDialog.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Mono.Unix;
using Habazie.kf2.Core;

namespace Habazie.kf2.GUI
{
	internal class Conf : Attribute
	{
		public string Key;
		public Conf (string k)
		{
			Key = k;
		}
	}

	
	public class SettingsDialog : Gtk.Dialog
	{
		[Conf ("auto-popup-events")]
		protected Gtk.CheckButton m_AutoPopup;
		
		[Conf ("nickname")]
		protected Gtk.Entry m_Nickname;
		
		[Conf ("hide-os-info")]
		protected Gtk.CheckButton m_HideOsInfo;
		
		[Conf ("auto-authorize")]
		protected Gtk.CheckButton m_AutoAuthorize;
		
		[Conf ("use-tabbed-interface")]
		protected Gtk.CheckButton m_UseTabbedInterface;
		
		[Conf ("single-window-interface")]
		protected Gtk.CheckButton SingleWindowInterface;
		
		[Conf ("enable-popup-notifications")]
		protected Gtk.CheckButton m_EnablePopups;
		
		[Conf ("sound-enabled")]
		protected Gtk.CheckButton m_EnableSound2;
		
		[Conf ("sound-use-external-player")]
		protected Gtk.CheckButton m_UseExternalPlayer3;
		
		[Conf ("sound-external-player")]
		protected Gtk.Entry m_PlayerCommand;
		
		
		protected Gtk.Entry m_SoundName;
		protected Gtk.TreeView m_Sounds;
		protected Gtk.ListStore m_Store;
		protected string m_Sound = null;
		protected Gtk.ScrolledWindow scrolledwindow1;

		
		public SettingsDialog(App a) : base ()
		{
			Stetic.Gui.Build(this, typeof(Habazie.kf2.GUI.SettingsDialog));
			scrolledwindow1.SetSizeRequest (5, 128);
			Autoconnect ();
			
			/* Setup sound treeview */
			m_Store = new Gtk.ListStore (typeof (string), typeof (string), typeof (string), typeof (string));
			m_Sounds.Model = m_Store;
			m_Sounds.AppendColumn ("Name",  new Gtk.CellRendererText (), "text", 1);
			m_Sounds.AppendColumn ("Sound", new Gtk.CellRendererText (), "text", 3);
			
			UpdateSounds ();
			
			m_Sounds.Selection.Changed += SelectionChanged;
		}
		
		private void UpdateSounds ()
		{
			m_Store.Clear ();
			AddSound ("online", Catalog.GetString ("User online"));
			AddSound ("offline", Catalog.GetString ("User offline"));
			AddSound ("chat-in", Catalog.GetString ("Incoming Chat"));
			AddSound ("chat-out", Catalog.GetString ("Outcoming chat"));
			AddSound ("message", Catalog.GetString ("Message"));
			AddSound ("system", Catalog.GetString ("System event"));
		}
		
		private void AddSound (string name, string desc)
		{
			string file = Util.GetSoundFile (name);
			m_Store.AppendValues (name, desc, file, System.IO.Path.GetFileName (file));
		}
		
		private void SelectionChanged (object sender, EventArgs args)
		{
			Gtk.TreeIter iter;
			Gtk.TreeModel model;
			if (m_Sounds.Selection.GetSelected (out model, out iter)) {
				Gtk.ListStore store = model as Gtk.ListStore;
				string soundname = (string) store.GetValue (iter, 0);
				string file = (string) store.GetValue (iter, 2);
				
				m_Sound = soundname;
				m_SoundName.Text = file;
			}
		}
		
		
		private void Autoconnect ()
		{
			System.Reflection.FieldInfo [] fields = this.GetType ().GetFields(((System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic) | System.Reflection.BindingFlags.Instance));
			foreach (System.Reflection.FieldInfo field in fields) {
				try {
					Conf c = (Conf) System.Attribute.GetCustomAttribute (field, typeof (Conf));
					string key = c.Key;
					object o = field.GetValue (this);
					if (o is Gtk.ToggleButton) {
					
						/* Toggle buttons connected with bool keys */
						
						Gtk.ToggleButton t = (Gtk.ToggleButton) o;
						t.Active = Util.Config.GetBool (key);
						t.Toggled += delegate {
							Console.Error.WriteLine ("Setting '{0}' to {1}", key, t.Active);
							Util.Config.SetBool (key, t.Active);
						};
					} else if (o is Gtk.Entry) {
					
						/* Entries connected with string keys */
						
						Gtk.Entry entry = (Gtk.Entry) o;
						entry.Text = Util.Config.GetString (key);
						entry.Changed += delegate {
							Util.Config.SetString (key, entry.Text);
						};
					}
				} catch (Exception e) {
					System.Console.Error.WriteLine ("Exception: {0}", e.Message);
				}
			}
		}
		
		

		protected virtual void OnCloseClicked(object sender, System.EventArgs e)
		{
			Destroy ();
		}

		protected virtual void OnSoundSelectClicked (object sender, System.EventArgs e)
		{
			Gtk.FileChooserDialog dialog = new Gtk.FileChooserDialog (
												"Choose sound file",
												this,
												Gtk.FileChooserAction.Open,
												Gtk.Stock.Cancel, Gtk.ResponseType.Cancel,
												Gtk.Stock.Open, Gtk.ResponseType.Accept);
			if (m_SoundName.Text != "")
				dialog.SetFilename (m_SoundName.Text);
			int response = dialog.Run ();
			string filename = dialog.Filename;
			Util.Debug ("GUI", "Response = {0}, filename = {1}", response, filename);
			dialog.Destroy ();
			if (response == (int) Gtk.ResponseType.Accept)
				m_SoundName.Text = filename;
		}

		protected virtual void OnSoundPlayClicked (object sender, System.EventArgs e)
		{
			Util.PlaySoundFile (m_SoundName.Text);
		}

		protected virtual void OnMSoundNameEditingDone (object sender, System.EventArgs e)
		{
			Util.Config.SetString ("sounds/"+m_Sound, m_SoundName.Text);
			UpdateSounds ();
		}
	}
}
